function database() {
  this.last_key = this.DEFAULT_NAME;
}

database.prototype.PREFIX = 'michurin.housepenbox.v1.';

database.prototype.DEFAULT_NAME = 'program1';

database.prototype.save_as = function(k, v) {
  this.last_key = k;
  localStorage.setItem(this.PREFIX + k, v);
};

database.prototype.remove = function(k) {
  this.last_key = this.DEFAULT_NAME;
  localStorage.removeItem(this.PREFIX + k);
};

database.prototype.list = function() {
  var c = [];
  var i = 0;
  var k;
  var l = this.PREFIX.length;
  var t;
  // Schwartzian transformation
  while (k = localStorage.key(i++)) {
    if (k.substr(0, l) === this.PREFIX) {
      t = k.substr(l);
      c.push([
        t.replace(/[0-9]+/g, '').toUpperCase(),
        parseInt(t.replace(/[^0-9]+/g, ''), 10),
        t
      ]);
    }
  }
  var s = c.sort(function(a, b) {
    for (var i = 0; i < 2; ++i) {
      if (a[i] < b[i]) {
        return -1;
      }
      if (a[i] > b[i]) {
        return 1;
      }
    }
    return 0;
  });
  var r = [];
  for (i = 0; i < s.length; ++i) {
    r.push(s[i][2]);
  }
  return r;
};

database.prototype.load = function(k) {
  this.last_key = k;
  return localStorage.getItem(this.PREFIX + k);
};

// --------------------------------------------------

function examples_database(e) {
  var data = {};
  var keys = [];
  e.children().each(function() {
    var t = $(this);
    var n = t.children('h1').text();
    var c = t.children('code').text();
    data[n] = c;
    keys.push(n);
  });
  this.data = data;
  this.keys = keys;
}

examples_database.prototype.list = function() {
  return this.keys;
};

examples_database.prototype.load = function(k) {
  return this.data[k];
};
